<?php

namespace app\zyhsapi\model;


use XznsTools\cache\zyhs\CacheManageZyhs;

/**
 * 提问模型类
 * Class Divination
 * @package app\common\model
 */
class ZyhsDivination extends BaseCacheZyhs
{
    protected $table = 'good_wechat.zyhs_divination';// 定义表名
    protected $cacheModel = 'divination.divination_record';

    /**
     * 骰子占卜信息写入
     * @param int $uid
     * @param array $data
     * @return false|int
     */
    public function saveData(int $uid, array $data)
    {
        try
        {
            $did = $this->insertGetId($data);

            $this->listDataAdd($uid, [$did => $did]);
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }

        return (int)$did;
    }

    /**
     * 更新 骰子 信息
     * @param $did
     * @param array $data 更新数据
     * @return bool
     */
    public function updateData($did, $data)
    {
        try
        {
            $this->where('id', $did)->update($data);
            CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel, $did)->setCache($data); // 更新详情缓存
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
        return true;
    }

    /**
     * 删除骰子占卜记录
     * @param int $uid 用户ID
     * @param int $did 骰子占卜记录ID
     * @return bool
     */
    public function delData(int $uid, int $did): bool
    {
        try
        {
            $this->where('id', $did)->update(['is_del' => 1]);
            $this->listDataDel($uid, [$did]); // 删除列表项
            CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel, $did)->rmCache(); // 删除详情缓存
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
        return true;
    }

    /**
     * 分页获取列表
     * @param array $pageData
     * @return array
     */
    public function pageDataReset(array $pageData)
    {
        $resData = [];

        foreach ($pageData as $itemData)
        {
            $resData[] = [
                'id' => (int)$itemData['id'],
                'question' => $itemData['question'],
                'planet' => (int)$itemData['planet'],
                'sign' => (int)$itemData['sign'],
                'house' => (int)$itemData['house'],
                'create_time' => date('Y-m-d H:i:s', $itemData['create_time']),
            ];
        }

        return $resData;
    }


    /**
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $this->where(['id' => ['in', $ids]]);
            } else
            {
                $this->where(['uid' => $type[0]]);
            }

            $data = $this->where('is_del', 0)->order("id {$sortType}")->column('uid,is_del,question,planet,sign,house,deg,create_time,timezone,longitude,latitude,status', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }


    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'uid', 'is_del', 'planet', 'sign', 'house', 'create_time', 'deg', 'timezone', 'status']);
        }

        return $data;
    }
}
